import { RootThunkAction } from '@/type/store'
import { ApiResponse, Token, LoginForm } from '@/type/data'
import request from '@/utils/request'
import { setToken } from '@/utils/storage'

export function login (data:LoginForm):RootThunkAction {
  return async (dispatch) => {
    const res = await request.post<ApiResponse<Token>>('/authorizations', data)
    console.log(res)
    setToken(res.data.data)
    dispatch({
      type: 'login/token',
      payload: res.data.data
    })
  }
}

// 获取验证码
export const getCode = (mobile: string) => {
  return async () => {
    await request.get(`/sms/codes/${mobile}`)

    // 注意：验证码是发送到手机上的，因此，不需要更新Redux状态
  }
}
